package civicinity.ist.psu.edu.rssfeed;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import civicinity.ist.psu.edu.R;
import civicinity.ist.psu.edu.constants.Constants;
import civicinity.ist.psu.edu.databases.CivicinityDatabase;
import civicinity.ist.psu.edu.utils.CivicinityDrawableManager;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.*;

public class ShowDescription extends Activity {
	
	private TextView newsTitle;
	private TextView newsInfo;
	private TextView newsDate;
	private TextView newsStory;
	private TextView newsLink;
	private ImageView newsImage;
	private CivicinityDatabase mDB;
	
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.show_description);
        
        String theStory = null;

        Intent startingIntent = getIntent();
        
        Bundle b = startingIntent.getBundleExtra("android.intent.extra.INTENT");
     
        newsTitle = (TextView) findViewById(R.id.news_title);
        newsDate = (TextView) findViewById(R.id.news_date);
        newsInfo = (TextView) findViewById(R.id.news_info);
        newsStory = (TextView) findViewById(R.id.news_story);
        newsLink = (TextView) findViewById(R.id.news_link);
        newsImage = (ImageView) findViewById(R.id.rss_image);
        
        // get the News information.
        mDB = new CivicinityDatabase(this);
        RSSDBItem rssInfo = new RSSDBItem();
        rssInfo = mDB.getRSSInfo(b.getInt("rssID"));
        
        // change the date format to user-friendly version.
        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm");
        Date resultdate = new Date(Long.parseLong(b.getString("pubdate")));
        
        newsTitle.setText(b.getString("title"));
        newsInfo.setText(rssInfo.getRSSName());
        newsDate.setText(sdf.format(resultdate));
        // set story TextView show the HTML format also.
        newsStory.setText(Html.fromHtml(changeToSpecialSymbols(b.getString("description"))));
        newsLink.setText("For more information : \n" + b.getString("link"));
        
        
        // set progressBar
		ProgressBar vBar = (ProgressBar) findViewById(R.id.progressbar);
		// set the RSS Image
		CivicinityDrawableManager dm = new CivicinityDrawableManager(ShowDescription.this, vBar, newsImage);
		// fetch the image
		dm.fetchDrawableOnThread(Constants.RSS_ICON_URL + rssInfo.getRSSUrl());
        
    }
    
    
    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }
    
	private String changeToSpecialSymbols(String aString) {
		
		String returnStr = aString;
		
		if(aString.contains("&ndash;")) {
			returnStr = returnStr.replaceAll("&ndash;", "-");
		}
		if(aString.contains("&quot;")) {
			returnStr = returnStr.replaceAll("&quot;", "\"");
		}
		if(aString.contains("&#39;")) {
			returnStr = returnStr.replaceAll("&#39;", "\'");
		}
		if(aString.contains("&rsquo;")) {
			returnStr = returnStr.replaceAll("&rsquo;", "\'");
		}
		if(aString.contains("&amp;")) {
			returnStr = returnStr.replaceAll("&amp;", "&");
		}
		if(aString.contains("<p>")) {
			returnStr = returnStr.replaceAll("<p>", "");
		}
		if(aString.contains("</p>")) {
			returnStr = returnStr.replaceAll("</p>", "");
		}
		if(aString.contains("<strong>")) {
			returnStr = returnStr.replaceAll("<strong>", "");
		}
		if(aString.contains("</strong>")) {
			returnStr = returnStr.replaceAll("</strong>", "");
		}
		if(aString.contains("&nbsp;")) {
			returnStr = returnStr.replaceAll("&nbsp;", " ");
		}
		if(aString.contains("\n")) {
			returnStr = returnStr.replaceAll("\n", " ");
		}
		
		
		Log.i("K", "returnStr : " + returnStr);
				
		return returnStr; 
	}
}
